'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class RunProcess

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal directiveStatus As IStatusDirective)
         Dim statusoutput As IStatusOutput
         Dim runProcess As CodeGenerationSupport.IRunProcessDirective
         Dim itemstatus As Utility.SourceControlBase.ItemStatus
         Dim outputFile As String
         Dim stream As IO.Stream = Nothing
         Dim obj As Object
         Dim params() As CodeGenerationSupport.XSLTParam
         Dim objs() As System.Object = New System.Object() {}
         Dim madeFile As Boolean
         Dim methodInfo As Reflection.MethodInfo
         runProcess = CType(node, CodeGenerationSupport.IRunProcessDirective)
         outputFile = runProcess.SinglePass.OutputFile
         statusoutput = directiveStatus.AddStatusOutput( _
                  outputFile, _
                  FileTreatment.Unknown)
         methodInfo = runProcess.Process.MethodInfo
         params = runProcess.Process.Params
         ' Added 2005-09-12 to support varying root filenames for the process that extracts Wind2's ER information
         Dim paramValues As String = runProcess.Parameter.Parameter
         If runProcess.Parameter.ExpandParameter Then
            paramValues = runProcess.ResolveFilePaths(paramValues)
            'Dim filePathNode As Xml.XmlNode = node.SelectSingleNode("//kg:FilePaths", Me.GetNsMgr)
            'runProcess.Parameter.Parameter = Utility.Tools.FixPath(runProcess.Parameter.Parameter, filePathNode)
         End If
         ' look for a better way to do this
         If params IsNot Nothing Then
            ReDim objs(params.GetUpperBound(0))
         End If
         For i As Int32 = 0 To objs.GetUpperBound(0)
            If params(i).Name.ToLower = "rundirective" Then
               objs(i) = node
            ElseIf params(i).Name.ToLower = "inputfilename" Then
               ' KAD 20070705: Changed to use the resolved path names based on harness file location
               Dim inputFileName As String = runProcess.InputFileName
               If Not inputFileName.Contains(":") Then
                  inputFileName = IO.Path.GetFullPath(IO.Path.Combine(My.Computer.FileSystem.CurrentDirectory, inputFileName))
               End If
               objs(i) = inputFileName
            ElseIf params(i).Name.ToLower = "outputfilename" Then
               objs(i) = runProcess.SinglePass.OutputFile
            Else
               objs(i) = Utility.Tools.GetAttributeOrEmpty(node, params(i).Name)
            End If
         Next
         If objs.Length > 0 Then
            If String.IsNullOrEmpty(objs(0).ToString()) Then
               objs(0) = paramValues
            End If
         End If
         Try
            If runUtility.ShouldRun(outputFile, _
                     runProcess.OutputRule.OutputGenType, _
                     runProcess.OutputRule.HashWhitespace, _
                     runProcess.OutputRule.OutputFileType, statusoutput) Then
               Try
                  obj = methodInfo.Invoke(Me, objs)
                  stream = TryCast(obj, IO.Stream)
                  If stream IsNot Nothing Then
                     madeFile = runUtility.MarkWriteAndClose(stream, _
                              runProcess.OutputRule.OutputFileType, _
                              runProcess.OutputRule.OutputGenType, _
                              runProcess.OutputRule.HashOutput, _
                              outputFile, statusoutput)
                  End If
               Finally
                  If Not stream Is Nothing Then
                     stream.Close()
                  End If
               End Try
            End If
            directiveStatus.CountCreated = 1
            directiveStatus.CountSelected = 1
            directiveStatus.CountOutput = 1
         Catch ex As System.Exception
            statusoutput.AddStatusException("CreateMetadata.Run", "Failure creating file", ex)
         End Try
         statusoutput.Status = statusoutput.ChildStatus
      End Sub
#End Region
   End Class
End Namespace